<template>
  <ul class="list">
    <li class="item" v-for='item of lettes' @click='handleLetterClick' :key='item'
    @touchstart.prevent='handleTouchStart' @touchmove='handleTouchMove' @touchend='handleTouchEnd' :ref='item'>{{item}}</li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphebet',
  props: {
    cities: Object
  },
  data () {
    return {
      iten: false,
      Adistance: 0,
      timer: null
    }
  },
  updated () {
    this.Adistance = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetterClick (el) {
      this.$emit('change', el.target.innerText)
    },
    handleTouchStart () {
      this.iten = true
    },
    handleTouchMove (e) {
      if (this.iten) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchDsitance = e.touches[0].clientY - 74
          const index = Math.floor((touchDsitance - this.Adistance) / 20)
          if (index >= 0 && index < this.lettes.length) {
            this.$emit('change', this.lettes[index])
          }
        }, 16)
      }
    },
    handleTouchEnd () {
      this.iten = false
    }
  },
  computed: {
    lettes () {
      const lettes = []
      for (let i in this.cities) {
        lettes.push(i)
      }
      return lettes
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    display flex
    flex-direction column
    justify-content center
    position absolute
    top 1.58rem
    right 0px
    bottom 0px
    width .4rem
    .item
      text-align: center
      line-height .4rem
      color: $bgColor
</style>
